package com.qfedu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qfedu.entity.AddressEntity;
import com.qfedu.entity.UserEntity;
import com.qfedu.kit.FruitsFinal;
import com.qfedu.kit.StringUtil;
import com.qfedu.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class AddressController {

    @Reference(check = false)
    AddressService addressService;
    //跳转常用地址
    @RequestMapping("jumpreceipt")
    public String jumpreceipt(HttpServletRequest request){

        //判断是否要显示对应信息
        AddressEntity ads = (AddressEntity) request.getSession().getAttribute("ads");
        if (ads==null){
            AddressEntity adss = new AddressEntity();
            adss.setAid("");
           adss.setSphone("");
           adss.setFphone("");
           adss.setSname("");
           adss.setFname("");
           adss.setSpath("");
           adss.setFpath("");
            request.getSession().setAttribute("ads",adss);
        }else {
            if (StringUtil.isEnpty(ads.getAid())) {
                ads.setAid("");
            }
            if (StringUtil.isEnpty(ads.getFname())) {
                ads.setFname("");
            }
            if (StringUtil.isEnpty(ads.getSname())) {
                ads.setSname("");
            }
            if (StringUtil.isEnpty(ads.getFpath())) {
                ads.setFpath("");
            }
            if (StringUtil.isEnpty(ads.getSpath())) {
                ads.setSpath("");
            }
            if (StringUtil.isEnpty(ads.getFphone())) {
                ads.setFphone("");
            }
            if (StringUtil.isEnpty(ads.getSphone())) {
                ads.setSphone("");
            }
            request.getSession().setAttribute("ads",ads);
        }
        //判断是否登录
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        if (user==null){
            request.setAttribute("address",null);
            return "receipt";
        }
        AddressEntity ae = new AddressEntity();
        ae.setUid(user.getUid());
        List<AddressEntity> queryaddress = addressService.queryaddress(ae);
        request.setAttribute("address",queryaddress);

        return "receipt";
    }

    //查询详细
    @RequestMapping("queryxx")
    public String query(String aid,HttpServletRequest request){
        AddressEntity queryad = addressService.queryad(aid);
        request.getSession().setAttribute("ads",queryad);
        return "redirect:jumpreceipt";
    }
    //修改信息
    @RequestMapping("updaddress")
    @ResponseBody
    public String updaddress(AddressEntity ae){
        boolean updaddress = addressService.updaddress(ae);
        if (updaddress){
            return FruitsFinal.SUCCESS_RESULT;//修改成功
        }

        return FruitsFinal.FILED_RESULT;//失败
    }

    //删除
    @RequestMapping("deladdress")
    @ResponseBody
    public String deladdress(String aid){

        boolean deladdress = addressService.deladdress(aid);
        if (deladdress){
            return FruitsFinal.SUCCESS_RESULT;//删除成功返回0000
        }
        return FruitsFinal.FILED_RESULT;//失败返回0001
    }

    //跳转到添加页面
    @RequestMapping("jumpaddressadd")
    public String jumpaddressadd(){

        return "addressadd";
    }

    //添加地址

    @RequestMapping("addd")
    @ResponseBody
    public String addaddress(AddressEntity ae){
        ae.setAid(StringUtil.getUUID().substring(0,12));
        boolean add = addressService.addaddress(ae);
        if (add){
            return FruitsFinal.SUCCESS_RESULT;//成功0000
        }
        return FruitsFinal.FILED_RESULT;//失败0001
    }

    //跳转客户告知书
    @RequestMapping("jumpInform")
    public String jumpinform(){

        return "Inform";
    }

}
